﻿using ApplicationModels.A03.Photographs;
using ApplicationModels.A04.Interactions;
using Commons.Responses;
using EntityModels.E03.Photographs;
using EntityModels.E04.Interactions;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class StarItemController : BaseController<int, StarItem, StarItemAM>
    {
        public StarItemController(IBaseApplicationService<int, StarItem, StarItemAM> apiService) : base(apiService)
        {
        }
        /// <summary>
        /// 获取收藏夹的作品
        /// </summary>
        /// <param name="starId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<DatasResponse<StarItemAM>> GetStarItemByStarIdAsync(int starId)
        {
            var star = (await ApiService.GetOtherAsync<int, Star, StarAM>(x => x.Id == starId)).Data;
            if (!star.IsPublicly)
            {
                if(star.AppUserId != AppUserId)
                {
                    return new DatasResponse<StarItemAM>();
                }
            }
            return await ApiService.GetAllAsync(x => x.StarId == starId);
        }

        public override async Task<MessageResponse> AddAsync(StarItemAM apiEntity)
        {
            var star = (await ApiService.GetOtherAsync<int, Star, StarAM>(x => x.Id == apiEntity.StarId && x.AppUserId == Guid.Parse(AppUserId))).Data;
            if(star == null)
            {
                return new MessageResponse("非法操作!", false);
            }
            var photograph = (await ApiService.GetOtherAsync<Guid, Photograph, PhotographAM>(p => p.Id == Guid.Parse(apiEntity.PhotographId))).Data;
            if(photograph == null)
            {
                return new MessageResponse("当前作品不存在!", false);
            }
            //star.UpdateTime = DateTime.Now;
            //await ApiService.UpdateOtherAsync<int, Star, StarAM>(star);
            return await base.AddAsync(apiEntity);
        }
        [HttpDelete]
        public async Task<MessageResponse> RemoveAsync(string photographId)
        {
            var data = (await ApiService.GetAsync(x => x.PhotographId == Guid.Parse(photographId) && x.Star.AppUserId == Guid.Parse(AppUserId))).Data;
            if(data == null)
            {
                return new MessageResponse("收藏不存在!", false);
            }
            var star = await ApiService.GetOtherAsync<int, Star, StarAM>(x => x.Id == data.StarId && x.AppUserId == Guid.Parse(AppUserId));
            if(star.Data == null)
            {
                return new MessageResponse("非法操作!", false);
            }
            return await ApiService.DeleteByIdAsync(data.Id);
        }
    }
}
